package com.powerunion.datacollection.transaction;

import javax.transaction.UserTransaction;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class JTATransaction implements Transaction{
    private UserTransaction transaction = null;
    private boolean active = false;
    public JTATransaction(){
    }

    public void begin()throws Exception{
        if(this.transaction == null){
            Context context = new InitialContext();
            this.transaction = (UserTransaction)context.lookup("java:comp/UserTransaction");
        }
        this.transaction.begin();
        this.active = true;
    }

    /**
     * commit
     *
     */
    public void commit()throws Exception{
        this.transaction.commit();
        this.active = false;
    }

    /**
     * rollback
     *
     */
    public void rollback()throws Exception{
        this.transaction.rollback();
        this.active = false;
    }

    public boolean isActive(){
        return active;
    }
}
